package com.example.ktapplication.dialog
import android.app.Dialog
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.*
import android.widget.TextView
import androidx.annotation.Nullable
import androidx.fragment.app.DialogFragment
import com.example.ktapplication.R


class DialogLoading private constructor() : DialogFragment() {

    private lateinit var mTip:String
    private lateinit var mTvTip:TextView


    companion object {
        val instance = SingletonHolder.holder
    }

    private object SingletonHolder {
        val holder= DialogLoading()
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        //创建dialog
        val dialog = Dialog(activity!!, R.style.BaseDialog)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        val window: Window = dialog.window!!
        //去掉dialog默认的padding
        window.getDecorView().setPadding(0, 0, 0, 0)
        val lp: WindowManager.LayoutParams = window.getAttributes()
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.MATCH_PARENT
        //设置dialog的位置
        lp.gravity = Gravity.CENTER
        window.setAttributes(lp)
        window.setBackgroundDrawable(ColorDrawable())
        return dialog
    }

    override fun onCreate(@Nullable savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.dialog_loading, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mTvTip=view.findViewById(R.id.tv_tip)
        mTvTip.setText(mTip)
    }

    override fun onDestroy() {
        super.onDestroy()
        dismiss()
    }
    /**
     * 设置提示语
     */
    public fun setTip(tip:String){
        this.mTip=tip
    }

}